<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Upsanpham extends CI_Controller {
    private $diachi='';// url cua dia chi can lay san pham
    private $list_folder='';// danh sach thu muc chua hinh anh
    function __construct()
	{
		parent::__construct();
        $this->load->library('session');  
		$this->load->helper(array('form','text','chuyendoitv_helper','url'));
		//$this->lang->load('tank_auth'); 
        $this->load->model('common_model');
        $this->load->library(array('pagination','form_validation','security'));
        
	}

	public function index(){
	   if(!empty($_POST)){
	      $sotrang = $_POST['num_page'];
          $diachi = $_POST['url'];
	      $dulieu = $_POST;
          $this->run($dulieu);
          if($sotrang){
            for($i=2;$i<=$sotrang;$i++){
                $dulieu['url']= $diachi.'&page='.$i;
                $this->run($dulieu);
            }
           // &page=
          } 
	   }else{
	       $this->run();
	   }       
     $this-> display();
	}
    function run($dulieu=''){  
      if(!empty($dulieu)){
        $this->diachi = $dulieu['url']; // url cua dia chi can lay san pham      
      //-> ghi hinh anh ra thu muc
        $dulieu_leech = $this->getdulieu();// lay giu lieu tu site
        foreach ($dulieu_leech as $mang){
            ///check alias            
            $alias=creat_alias($mang['title']);                  
            $check =$this->common_model->check_trung(array('alias'=>$alias),'product');  
            if($check){$alias = $alias.$mang['code'];} 
            $mang['alias']=$mang['alias_en']=$alias;
            //url dia chi hinh anh
            //image duong dan+ ten anh
            $url ="http://www.easytraded.com/".trim($mang['image']);// link anh goc
            $phanmorong=$this->get_file_extension(trim($mang['image']));
            $image = $dulieu['folder'].'/'.trim($mang['code']).'.'.$phanmorong;// la dia chi anh cho trang luon(lay id noi voi phan mo rong ra ten anh)
            $mang['image'] = $image;
            $mang['category'] = $dulieu['cat'];
            $mang['manufacturer'] = $dulieu['nhasanxuat'];
            $update_mang[]=$mang; // du lieu da duoc xu ly link anh           
            $this->save_img($url,$image);  //lu lai hinh anh vao thu muc         
          } 
         $this->db->insert_batch('product', $update_mang); 
      }
      // them du lieu vao $mag va insert vao data   
    }
    
    function display(){
         //// lay list danh muc
        $this->laythumuc('upload');// thu muc goc
        //$this->list_folder;// danh sach danh muc 
        $cat = $this->common_model->getdatain('product_category');      
        $nhasanxuat = $this->common_model->getdatain('product_manufacturer');
      
        $data=array(
            'list_folder'=>$this->list_folder,
            'cat'=>$cat,
            'nhasanxuat'=>$nhasanxuat
            
            );
        $this->load->view('upsanpham',$data);
    }
    function getdulieu(){
        $mang = '';
        $file=file_get_contents($this->diachi);
        $begin="<table class=\"picb\" align=center width=150>";
        $end="<\/table>";
        preg_match_all("/$begin.*$end/msU",$file,$kq1);  
        $kq1= $kq1['0'];
        // lay duoc du lieu day sang cac ham su ly
        $dieukhien = 0;
        foreach ($kq1 as $kq11){
         $t= $this->getid($kq11); 
         $mang[$dieukhien]['code']=$t[0];
         $t= $this->getanh($kq11); 
         $mang[$dieukhien]['image']=$t[0];
         $t= $this->gettitle($kq11); 
         $mang[$dieukhien]['title']=$mang[$dieukhien]['title_en']=$t[0];
         $t= $this->getprice($kq11); 
         $mang[$dieukhien]['price']=$t[0];
         $dieukhien++;          
        }
        return $mang;
        // $alias = preg_replace("/(.htm)/", '', $alias);
    }
    
    function laythumuc($dir){
        $this->list_folder.= '<select name="folder">';
        $this->getthumuc($dir);
        $this->list_folder.= '</select>';
        
    }
    function getthumuc($td=''){
       foreach(glob("./$td/*", GLOB_ONLYDIR) as $dir){
            $dir = basename($dir);
            $this->list_folder.= '<option value="'. $td.'/'.$dir.'">'. $td.'/'.$dir. '</option>';
            $t=glob("./$td/$dir/*", GLOB_ONLYDIR);
            if(!empty($t)){                
                $this->getthumuc($td.'/'.$dir);                
            }
        }       
    }
    ////
    function getanh($kq1){
       $kq1;
        $begin="src=\"";
        $end="\"";
       preg_match("/$begin.*$end/msU",$kq1,$kq);
       $kq = preg_replace("/(src=\")/", '', $kq); 
       $kq = preg_replace("/(\")/", '', $kq); 
       $kq[0]=trim(strip_tags($kq[0]));
       return $kq;
    }
    function gettitle_id($kq1){
       $kq1;
        $begin="<td class=\"titleb\">";
        $end="<\/td>";
        preg_match("/$begin.*$end/msU",$kq1,$kq); 
        $kq[0]=strip_tags($kq[0]);
        return $kq;
    }
    function getid($kq1){
       $kq1;
        $begin="<td class=\"titleb\">";
        $end="<\/td>";
        preg_match("/$begin.*$end/msU",$kq1,$kq);    
       
        $begin="ID:";
        $end="\<br \/>";
        preg_match("/$begin.*$end/msU",$kq[0],$kq); 
        
        $kq = preg_replace("/(ID:)/", '', $kq);
        $kq[0]=trim(strip_tags($kq[0]));
        return $kq;
    }
    function gettitle($kq1){
        $kq1;
        $begin="<td class=\"titleb\">";
        $end="<\/td>";
        preg_match("/$begin.*$end/msU",$kq1,$kq);     
        $begin="\<br \/>";
        $end="<\/a>";
        preg_match("/$begin.*$end/msU",$kq[0],$kq2); 
        $kq2[0]=trim(strip_tags($kq2[0]));
        return $kq2;
    }
    function getprice($kq1){
        $kq1;
        $begin="\<span style=\"color:red;\">";
        $end="<\/span>";
        preg_match("/$begin.*$end/msU",$kq1,$kq);
        //$kq[0] = preg_replace("/($)/", '', $kq[0]); 
        $kq = preg_replace("/(<span style=\"color:red;\">＄)/", '', $kq); 
        $kq = preg_replace("/(.00)/", '', $kq);
        //$kq= $kq+100;
        $kq[0]=trim(strip_tags($kq[0]));    
        return $kq;
    }
    function save_img($url,$img){
      //  $url = 'http://ni5.upanh.com/b4.s13.d4/6eef889f83c23040217d880d2ad1207f_40758945.dsc00050.jpg';
       // $img = 'upload/flower.gif';//duong dan anh
       // file_put_contents($img, file_get_contents($url));
        //echo 'ok';
        
        //$url = 'http://ni5.upanh.com/b4.s13.d4/6eef889f83c23040217d880d2ad1207f_40758945.dsc00050.jpg';
        //$img = 'upload/flower.gif';//duong dan anh// lay duong dan noi them ten hinh anh
        file_put_contents($img, file_get_contents($url));      
        
        
       // $img=$_SERVER['DOCUMENT_ROOT'].'/test/getcontent/test.jpg';// de test khong dung
       
    }
    function get_file_extension($file_name) {
        return substr(strrchr($file_name,'.'),1);
    }


}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */